<template>
  <div class="home-container">
    <!-- 顶部按钮 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /顶部按钮 -->

    <!-- 频道列表 -->
    <van-tabs v-model="active" animated swipeable class="channel-tabs">
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <!-- 把列表项传给子组件 -->
        <ArticleList :channel="item"></ArticleList>
      </van-tab>

      <!-- 占位用的元素 -->
      <div slot="nav-right" class="placeholder"></div>

      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道列表 -->

    <!-- 弹框都放在下面 -->
    <!-- 频道编辑 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <!-- 弹出框中的内容 -->
      <ChannelEdit
        :my-channels="channels"
        :active="active"
        @update-active="onUpdateActive"
      ></ChannelEdit>
    </van-popup>
    <!-- /频道编辑 -->
  </div>
</template>
<script>
import { getUserChannels } from "@/api/user.js";
import ArticleList from "./components/article-list";
import ChannelEdit from "./components/channel-edit";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage.js";
export default {
  name: "HomeIndex",
  data() {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false, //控制编辑弹出层的状态  false隐藏  true 显示;
    };
  },
  created() {
    this.loadChannels();
  },
  methods: {
    async loadChannels() {
      try {
        // const {
        //   data: {
        //     data: { channels },
        //   },
        // } = await getUserChannels();

        // this.channels = channels; //把请求到的数据给channels 赋值;
        let channelsArr = [];
        if (this.user) {
          //已经登录则请求
          const {
            data: {
              data: { channels },
            },
          } = await getUserChannels();
          channelsArr = channels;
        } else {
          //没有登录则获取本地 本地没有 则 请求默认数据;
          const localChannels = getItem("TOUTIAO_CHANNELS");
          if (localChannels) {
            //本地有 拿来用;
            channelsArr = localChannels;
          } else {
            //没有则请求
            const {
              data: {
                data: { channels },
              },
            } = await getUserChannels();

            channelsArr = channels;
          }
        }
        this.channels = channelsArr;

        /*
          已经登录 -->发起请求
          未登录 
              本地有数据 获取本地数据
              本地没有数据 请求默认数据;
        */
      } catch (err) {
        this.$toast("请求失败!");
      }
    },
    onUpdateActive(index, isChannelEditShow = true) {
      // 点击跳转到指定的列表;
      this.active = index;
      //  关闭编辑频道弹框;
      this.isChannelEditShow = isChannelEditShow;
    },
  },
  components: {
    ArticleList,
    ChannelEdit,
  },
  computed: {
    ...mapState(["user"]),
  },
};
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  padding-top: 174px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      height: 82px;
      border-bottom: 1px solid #dedff3;
    }
    .van-tab {
      border-right: 1px solid #dedff3;
      min-width: 200px;
      font-size: 30px;
      color: #777;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
      bottom: 8px;
    }
  }

  // ===============汉堡按钮
  .hamburger-btn {
    position: fixed;
    right: 0;
    background-color: rgba(255, 255, 255, 0.6);
    width: 66px;
    height: 82px;
    display: flex;
    justify-content: center;
    align-items: center;
    .toutiao {
      font-size: 33px;
    }
  }
  // 这个是一个占位元素;
  .placeholder {
    // min-width: 66px;
    width: 66px;
    height: 82px;
    flex-shrink: 0; //不参与弹性盒子计算
    background-color: #fff;

    //设置了width:66px;后没有效果 有两种解决方法
    // 因为父元素设置了display:flex;
    //1.解决 flex-shrink:0;不参与弹性盒子计算
    //2.设置min-width:66px; 两种解决方法;
  }

  /deep/ .van-tabs__wrap {
    position: fixed;
    top: 92px !important;
    left: 0;
    right: 0;
    top: 0;
    z-index: 2;
  }

  /deep/ .van-popup__close-icon {
    color: #666 !important;
  }
}
</style>
